package cn.kepu.elearning.dto.match;

import cn.kepu.elearning.common.Constants;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Endy
 * @create 2019-12-12 上午9:12
 **/
@Getter
@Setter
@ToString(callSuper = true)
public class MatchQueryTeacherDTO extends CommonQueryDTO {

    private String hireStyle;
    private String expertAreaId;
    private String tenantId;
    private String ifTenantId;
    private List<String> orgSeqList;
    /**
     * 值为1则查询课程形式，值为2则不查询课程形式
     */
    private Integer searchType;


    public MatchQueryDTO convert() {
        MatchQueryDTO matchQueryDTO = new MatchQueryDTO();
        matchQueryDTO.setType(Constants.Type.TEACHER);
        matchQueryDTO.setPageNum(this.getPageNum());
        matchQueryDTO.setPageSize(this.getPageSize());
        matchQueryDTO.setFilterText(this.getFilterText());
        matchQueryDTO.setFieldNames(this.getFieldNames());
        matchQueryDTO.setOther(generatorOther());
        return matchQueryDTO;
    }

    private List<OtherQueryDTO> generatorOther() {
        List<OtherQueryDTO> other = new ArrayList<>();
        if (StringUtils.isNotBlank(this.getHireStyle())) {
            OtherQueryDTO other0 = new OtherQueryDTO();
            other0.setField("hireStyle");
            other0.setFieldType("number");
            other0.setValue(this.getHireStyle());
            other0.setOperator(Constants.Operator.TERM);
            other0.setLogicOperator(Constants.LogicOperator.MUST);
            other.add(other0);
        }
        if (StringUtils.isNotBlank(this.getExpertAreaId())) {
            OtherQueryDTO other1 = new OtherQueryDTO();
            other1.setField("expertAreaId");
            other1.setFieldType("number");
            other1.setValue(this.getExpertAreaId());
            other1.setOperator(Constants.Operator.TERM);
            other1.setLogicOperator(Constants.LogicOperator.MUST);
            other.add(other1);
        }
        if (StringUtils.isNotBlank(this.getIfTenantId()) && StringUtils.isNotBlank(this.getTenantId())) {
            if ("2701".equals(this.getIfTenantId())) {
                OtherQueryDTO other3 = new OtherQueryDTO();
                other3.setField("tenantId");
                other3.setFieldType("number");
                other3.setValue(this.getTenantId());
                other3.setOperator(Constants.Operator.TERM);
                other3.setLogicOperator(Constants.LogicOperator.MUST);
                other.add(other3);
            } else if ("2702".equals(this.getIfTenantId())) {
                OtherQueryDTO other3 = new OtherQueryDTO();
                other3.setField("tenantId");
                other3.setFieldType("number");
                other3.setValue(this.getTenantId());
                other3.setOperator(Constants.Operator.TERM);
                other3.setLogicOperator(Constants.LogicOperator.MUST_NOT);
                other.add(other3);
            }
        }
        if (!CollectionUtils.isEmpty(this.getOrgSeqList())) {
            OtherQueryDTO other5;
            for (String s : this.getOrgSeqList()) {
                other5 = new OtherQueryDTO();
                other5.setField("openOrgSEQ");
                other5.setFieldType("string");
                other5.setValue(s);
                other5.setOperator(Constants.Operator.TERM);
                other5.setLogicOperator(Constants.LogicOperator.SHOULD);
                other.add(other5);
            }
        }
        return other;
    }
}
